package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsArticle;
import com.qf.service.DtsArticleService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zwnan
 */
@RestController
@RequestMapping("/admin/article")
@CrossOrigin
public class ArticleController {

    @Autowired
    private DtsArticleService dtsArticleService;

    @RequiresPermissionsDesc(menu = {"系统管理","推广管理"},button = "广告列表")
    @RequiresPermissions(value = {"admin:article:list"})
    @GetMapping("/list")
    public Object articleList(@RequestParam(defaultValue = "1")Integer page,
                              @RequestParam(defaultValue = "20") Integer limit,
                              @RequestParam(defaultValue = "add_time") String sort,
                              @RequestParam(defaultValue = "desc") String order){

        IPage<DtsArticle> iPage = dtsArticleService.findPage(page,limit,sort,order);
        long total = iPage.getTotal();
        List<DtsArticle> records = iPage.getRecords();

        Map map = new HashMap();
        map.put("total", total);
        map.put("items", records);
        return ResponseUtil.ok(map);

    }
    @RequiresPermissionsDesc(menu = {"系统管理","推广管理"},button = "发布公告")
    @RequiresPermissions(value = {"admin:article:create"})
    @PostMapping("/create")
    public Object create(@RequestBody DtsArticle dtsArticle){
        dtsArticleService.create(dtsArticle);
        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"系统管理","推广管理"},button = "更新公告")
    @RequiresPermissions(value = {"admin:article:update"})
    @PostMapping("/update")
    public Object update(@RequestBody DtsArticle dtsArticle){
        dtsArticleService.update(dtsArticle);
        return ResponseUtil.ok();
    }


    @RequiresPermissionsDesc(menu = {"系统管理","推广管理"},button = "删除公告")
    @RequiresPermissions(value = {"admin:article:delete"})
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsArticle dtsArticle){
        dtsArticleService.delete(dtsArticle);
        return ResponseUtil.ok();
    }

}
